﻿using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Rud.Holmgren.Tabulex3000.Util
{
    class SystemTrayProgressManager : IProgressManager
    {
        private ProgressIndicator pind;
        private Dispatcher dispatcher;

        public SystemTrayProgressManager(DependencyObject page)
        {
            this.dispatcher = page.Dispatcher;

            this.pind = new ProgressIndicator();

            SystemTray.SetIsVisible(page, true);
            SystemTray.SetOpacity(page, 0);
            SystemTray.ProgressIndicator = this.pind;
        }

        public void SetState(bool isvisible, string caption)
        {
            if (!dispatcher.CheckAccess())
            {
                dispatcher.BeginInvoke(() => SetState(isvisible, caption));
                return;
            }

            pind.IsIndeterminate = true;
            pind.IsVisible = isvisible;

            if (isvisible)
            {
                pind.Text = caption;
            }
        }

    }
}
